#!/usr/bin/env bash

result=`adb devices`
result=${result/'List of devices attached'/}
result=${result//'device'/}
result=`echo $result | tr -s '\n'`
# echo -e "device list: \n$result"
 
array=(`echo -e $result | tr '\n' ' '`)

declare -a modelArr
for(( i=0;i<${#array[@]};i++)) do
    # echo "$i: ${array[i]}"
    modelArr[$i]=`adb -s ${array[i]} shell getprop ro.product.model`
done;

if test ${#array[*]} -gt 1 
then
    for(( i=0;i<${#array[@]};i++)) do
        echo "$i: ${array[i]} ${modelArr[i]}"
    done;
    read -p "more than one device! select one:" index
    # echo "index = $index, ${array[index]}"
    if test $index -ge ${#array[*]}
    then
        echo "wrong index!"
    else
        adb -s ${array[index]} $*
    fi
else
    echo "one device!"
    if test $# -gt 0
    then
        adb $*
    fi
fi